
const http = require("http")
const url = require("url")
const { fileURLToPath, urlToHttpOptions} = require('node:url');

const ModuleRenderStatus = require("./module/renderStatus.js")
const ModuleRenderHTML = require("./module/renderHtml.js")
// 创建服务器的专门核心代码
/*http.createServer((req, res)=> {
  // req 接收浏览器传的参数
 if (req.url === "/favicon.ico") {
   return
 }
  console.log(req.url)
  // res 返回渲染内容
  res.writeHead(ModuleRenderStatus.renderStatus(req.url), {"Content-Type": "text/html;charset=utf-8"})
  res.write(ModuleRenderHTML.renderHTML(req.url)) // 往浏览器输出东西
  res.end() // 只能传字符串

}).listen(3001, ()=> {
  console.log("server start")
})*/

// 与下面这种方法一样
const server = http.createServer()
server.on("request", (req,res)=> {
  if (req.url === "/favicon.ico") {
    return
  }
 const myURL = new URL(req.url, 'http://127.0.0.1:3001')
  console.log(myURL.searchParams.get('a'))
  for (var [key, value] of myURL.searchParams) {
    console.log(key,value)
  }
  // res 返回渲染内容
  res.writeHead(ModuleRenderStatus.renderStatus(myURL.pathname), {"Content-Type": "text/html;charset=utf-8"})
  res.write(ModuleRenderHTML.renderHTML(myURL.pathname)) // 往浏览器输出东西
  res.end() // 只能传字符串
})

server.listen(3001,()=> {
  console.log("server start")
})

const urlString = 'https://www.baidu.com:443/ad/index.html?id=8&name=mouse#tag=110'
const parsedStr = url.parse(urlString)
console.log(parsedStr)

// const url111 =  {
//   protocol: 'https:',
//     slashes: true,
//     auth: null,
//     host: 'www.baidu.com:443',
//     port: '443',
//     hostname: 'www.baidu.com',
//     hash: '#tag=110',
//     search: '?id=8&name=mouse',
//     query: 'id=8&name=mouse',
//     pathname: '/ad/index.html',
//     path: '/ad/index.html?id=8&name=mouse',
//     href: 'https://www.baidu.com:443/ad/index.html?id=8&name=mouse#tag=110'
// }
//
// console.log(url.format(url111))

// const a = url.resolve('/one/two/three', 'four') // 注意最后加/,和不加/的区别
// const b = url.resolve('http://example.com/one/two/three/', '/four') // 注意最后加/,和不加/的区别
//
// /** 加/ 是拼接，不加/是替换, 后面的加/就是替换*/
// console.log(a)
// console.log(b)


// const myURL =
//   new URL('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');
// res.writeHead()

var b = new URL('four', 'http://example.com/one/two/three/')
console.log(b.href)

var aaa = new URL('https://a:b@测试?abc#foos')

console.log(url.format(aaa,{unicode: true,auth: false,fragment: false,search: false}))

new URL('file:///你好.txt').pathname;
fileURLToPath('file:///你好.txt');
console.log("------------------- ")
console.log(new URL('file:/Users/danyang.zhang/Documents').pathname)
console.log(fileURLToPath('file:/Users/danyang.zhang/Documents'))



const myURL = new URL('https://a:b@測試?abc#foo');
console.log(myURL)
console.log(urlToHttpOptions(myURL));